#include "ContextMenu.h"



Context::Context()
{
	isActive = false;
}

void Context::release()
{

}

void Context::init(BeanieCore* a_core)
{
	m_core = a_core;
	setClicked(V2D_ZERO);
	m_clickable.m_core = m_core;
	backdrop.initTexture(m_core,"sprites/DropDown.png",TEX_CENTER,V2D_ZERO, V2D_ONES,0);
}

void Context::update()
{
	backdrop.update();
}

bool Context::input()
{
	if(isActive)
	{
		if(m_core->keyPress(KEY_ESCAPE))
		{
			setStatus(false);
			return true;
		}
		if(m_core->mousePress(BTN_LEFT) || m_core->mousePress(BTN_RIGHT)
			|| m_core->mousePress(BTN_MIDDLE))
		{
			if(!m_clickable.isClicked(BTN_LEFT) && !m_clickable.isClicked(BTN_RIGHT)
				&& !m_clickable.isClicked(BTN_MIDDLE))
			{
				isActive = false;
			}
		}
	}
	return false;
}

void Context::draw()
{
	int color = 0xffff0000;
	if(isActive)
	{
		backdrop.draw();
		m_core->drawLine(V2DF(m_clickable.LEFT,m_clickable.TOP),	V2DF(m_clickable.RIGHT,m_clickable.TOP),color);
		m_core->drawLine(V2DF(m_clickable.RIGHT,m_clickable.TOP),	V2DF(m_clickable.RIGHT,m_clickable.DOWN),color);
		m_core->drawLine(V2DF(m_clickable.RIGHT,m_clickable.DOWN),	V2DF(m_clickable.LEFT,m_clickable.DOWN),color);
		m_core->drawLine(V2DF(m_clickable.LEFT,m_clickable.TOP),	V2DF(m_clickable.LEFT,m_clickable.DOWN),color);
		for (int i = 0; i < m_menu.size(); i++)
		{
			m_core->drawText(m_menu[i],V2DF(clickables[i]->LEFT,clickables[i]->TOP), 0xffff0000);
		}
	}
}

void Context::setClicked(V2DF a_clicked)
{
	clicked = a_clicked;
	m_clickable.DOWN	= clicked.y+(CHAR_HEIGHT*m_menu.size());
	m_clickable.TOP		= clicked.y;
	m_clickable.RIGHT	= clicked.x+(CHAR_WIDTH*MAX_LEN);
	m_clickable.LEFT	= clicked.x;
	float cenX = (m_clickable.LEFT+m_clickable.RIGHT)/2.0f;
	float cenY = (m_clickable.DOWN+m_clickable.TOP)/2.0f;
	float sclX = (m_clickable.RIGHT-m_clickable.LEFT)/2.0f;;
	float sclY = (m_clickable.DOWN-m_clickable.TOP)/2.0f;
	backdrop.m_pos = V2DF(cenX,cenY);
	backdrop.m_scl = V2DF(sclX,sclY);
	for (int i = 0; i < clickables.size(); i++)
	{
		
		clickables[i]->DOWN	= clicked.y+((CHAR_HEIGHT)+CHAR_HEIGHT*i/*clickables.size()*/);
		clickables[i]->TOP	= clicked.y+(CHAR_HEIGHT*i);
		clickables[i]->RIGHT= clicked.x+(CHAR_WIDTH*MAX_LEN);
		clickables[i]->LEFT	= clicked.x;
	}
}

void Context::addMenuItem(char* a_in)
{
	m_menu.add(a_in);
	Clickable* newClick = new Clickable;
	newClick->m_core= m_core;
	clickables.add(newClick);
}